{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "shL4Sf5bTuz1"
   },
   "source": [
    "# Harmonic Trading Points"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KddheTjnTuz2"
   },
   "source": [
    "## Description"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "AygI3WGZTuz2"
   },
   "source": [
    "Harmonic trading patterns are comprised of five points X, A, B, C, and D.  In this function, the user must supply the X point (the starting price of the pattern), the A point (the ending price of the first move in the pattern), the XAB ratio (the percentage of the B point relative to points X and A), the ABC ratio (the percentage of the C point relative to points A and B), the BCD ratio (the percentage of the D point relative to points B and C), the XAD ratio (the percentage of the D point relative to points X and A), and the AB=CD ratio (the percentage of the D point relative to points A and B).  The output is a five point harmonic trading pattern or five points in currency terms and a potential reversal zone (PRZ) which encompasses D, XAD, and ABCD."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9TNIfBHRTuz7"
   },
   "source": [
    "## Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "executionInfo": {
     "elapsed": 582,
     "status": "ok",
     "timestamp": 1613239217716,
     "user": {
      "displayName": "Gregory DeSantis",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhfgiOHX-RDVNEJj8rSPrG1hgIlx8g86t1lYdXBW7s=s64",
      "userId": "13988622607574133301"
     },
     "user_tz": 300
    },
    "id": "cyXb75xCTuz8"
   },
   "outputs": [],
   "source": [
    "def points(X: float, A: float, XAB_ratio: float, ABC_ratio: float, \\\n",
    "           BCD_ratio: float, XAD_ratio: float, ABCD_ratio: float) -> None:\n",
    "    \"\"\"Given the X point, A point, XAB ratio, ABC ratio, BCD ratio, XAD ratio,\n",
    "    and AB=CD ratio, get the X point, A point, B point, C point, D point,\n",
    "    XAD point, and ABCD point\"\"\"\n",
    "    assert X > 0, \"X must be greater than 0\"\n",
    "    assert A > 0, \"A must be greater than 0\"\n",
    "    assert 0.382 <= XAB_ratio <= 3.618, \"Ratios must be between 0.382 and 3.618\"\n",
    "    assert 0.382 <= ABC_ratio <= 3.618, \"Ratios must be between 0.382 and 3.618\"\n",
    "    assert 0.382 <= BCD_ratio <= 3.618, \"Ratios must be between 0.382 and 3.618\"\n",
    "    assert 0.382 <= XAD_ratio <= 3.618, \"Ratios must be between 0.382 and 3.618\"\n",
    "    assert 0.382 <= ABCD_ratio <= 3.618, \"Ratios must be between 0.382 and 3.618\"\n",
    "    B = A - ((A - X) * XAB_ratio)\n",
    "    C = B - ((B - A) * ABC_ratio)\n",
    "    D = C - ((C - B) * BCD_ratio)\n",
    "    XAD = A - ((A - X) * XAD_ratio)\n",
    "    ABCD = C - ((A - B) * ABCD_ratio)\n",
    "    print(f\"points: X={format(X, '.4f')}, A={format(A, '.4f')}, \\\n",
    "B={format(B, '.4f')}, C={format(C, '.4f')}, D={format(D, '.4f')}\")\n",
    "    print()\n",
    "    print(f\"potential reversal zone: D={format(D, '.4f')}, \\\n",
    "XAD={format(XAD, '.4f')}, ABCD={format(ABCD, '.4f')}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jGzcWPX0Tuz-"
   },
   "source": [
    "## Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 578,
     "status": "ok",
     "timestamp": 1613239217717,
     "user": {
      "displayName": "Gregory DeSantis",
      "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GhfgiOHX-RDVNEJj8rSPrG1hgIlx8g86t1lYdXBW7s=s64",
      "userId": "13988622607574133301"
     },
     "user_tz": 300
    },
    "id": "aQIawM9kTuz-",
    "outputId": "16597b10-b9f8-454e-8652-42330273686c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "points: X=1460.0000, A=1520.0000, B=1490.0000, C=1508.5400, D=1471.4600\n",
      "\n",
      "potential reversal zone: D=1471.4600, XAD=1466.8400, ABCD=1470.4400\n"
     ]
    }
   ],
   "source": [
    "points(1460, 1520, 0.5, 0.618, 2.0, 0.886, 1.27)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Harmonic Trading Points.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
